package com.naxiangfish.cn.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.okgo.db.DownloadManager;
import com.lzy.okserver.OkDownload;
import com.lzy.okserver.download.DownloadTask;
import com.naxiangfish.cn.R;
import com.naxiangfish.cn.been.DownLoadeBeen;

import java.util.List;

/**
 * 作者: 赵航
 * 创建时间: 2018/9/7 16:51
 * 描述:
 */
public class DownLoadeAdapter extends BasicRecycleViewAdapter<DownLoadeBeen> {

    private ImageView    mHandImage;
    private TextView     mFileTitle;
    private Button       mFileStop;
    private ProgressBar  mProgressBar;
    private Context      mContext;


    public static final int TYPE_ALL = 0;
    public static final int TYPE_FINISH = 1;
    public static final int TYPE_ING = 2;

    private int type;


    private List<DownloadTask> values;

    public DownLoadeAdapter(List<DownLoadeBeen> showItems, Context context) {
        super(showItems);
        mContext = context;
    }

    @Override
    public void convert(BasicViewholder holder, final DownLoadeBeen itm, final int position) {
        mHandImage = (ImageView) holder.getView(R.id.handImage);
        mFileTitle = (TextView) holder.getView(R.id.filetitle);
        mFileStop = (Button) holder.getView(R.id.filestop);
        mProgressBar = (ProgressBar) holder.getView(R.id.filerogress);

        Glide.with(mContext)
                .asBitmap()
                .load(itm.getImageUri())
                .apply(getRequestOptions())
                .into(mHandImage);

        mFileTitle.setText(itm.getName());

        mFileStop.setText(itm.getText());



        mFileStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public int getSuccessFullView(int viewType) {
        return R.layout.item_list_down;
    }

    private RequestOptions getRequestOptions() {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.ic_launcher);
        options.error(R.mipmap.ic_launcher);

        return options;

    }


    public void updateData(int mType) {
        //这里是将数据库的数据恢复
        this.type = mType;
        if (type == TYPE_ALL) values = OkDownload.restore(DownloadManager.getInstance().getAll());
        if (type == TYPE_FINISH) values = OkDownload.restore(DownloadManager.getInstance().getFinished());
        if (type == TYPE_ING) values = OkDownload.restore(DownloadManager.getInstance().getDownloading());
        notifyDataSetChanged();
    }


}
